package com.dhcc.bpm.modules.auth.granter;

import lombok.AllArgsConstructor;
import org.springblade.core.secure.exception.SecureException;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName TokenGranterBuilder
 * @Description 刷新token
 * @Author xiafayu
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@AllArgsConstructor
public class TokenGranterBuilder {


    /**
     * TokenGranter缓存池子
     */
    private static Map<String, ITokenGranter> granterPool = new ConcurrentHashMap<>();

    static {
        granterPool.put(PasswordTokenGranter.GRANT_TYPE, SpringUtil.getBean(PasswordTokenGranter.class));
        granterPool.put(RefreshTokenGranter.GRANT_TYPE, SpringUtil.getBean(RefreshTokenGranter.class));
        granterPool.put(CaptchaTokenGranter.GRANT_TYPE, SpringUtil.getBean(CaptchaTokenGranter.class));
    }

    /**
     * @param grantType 授权类型
     * @return com.dhcc.bpm.modules.auth.granter.ITokenGranter
     * @Description 获取TokenGranter
     * @Author xiafayu
     * @Date 2020/2/24 17:41
     **/
    public static ITokenGranter getGranter(String grantType) {
        ITokenGranter tokenGranter = granterPool.get(Func.toStr(grantType, PasswordTokenGranter.GRANT_TYPE));
        if (tokenGranter == null) {
            throw new SecureException("no grantType was found");
        } else {
            return granterPool.get(Func.toStr(grantType, PasswordTokenGranter.GRANT_TYPE));
        }
    }

}
